/*
 * Copyright (C) 2023 Coder.AN
 * Email: an.hongjun@foxmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "edge_tensor.h"
#include "edge_tensor_kernel.h"
#include <stdio.h>

namespace resize{
__device__ void cal_dim(int idx, int* n, int* h, int* w, int*c, Dim dim, DimOrder order)
{
    if (order == NHWC)
    {
        *n = idx / (dim.H * dim.W * dim.C);
        idx -= *n * dim.H * dim.W * dim.C;
        *h = idx / (dim.W * dim.C);
        idx -= *h * dim.W * dim.C;
        *w = idx / dim.C;
        idx -= *w * dim.C;
        *c = idx;
    }
    else if (order == NCWH)
    {
        *n = idx / (dim.C * dim.W * dim.H);
        idx -= *n * dim.C * dim.W * dim.H;
        *c = idx / (dim.W * dim.H);
        idx -= *c * dim.W * dim.H;
        *w = idx / dim.H;
        idx -= *w * dim.H;
        *h = idx;
    }
    else if (order == NCHW)
    {
        *n = idx / (dim.C * dim.H * dim.W);
        idx -= *n * dim.C * dim.H * dim.W;
        *c = idx / (dim.H * dim.W);
        idx -= *c * dim.H * dim.W;
        *h = idx / dim.W;
        idx -= *h * dim.W;
        *w = idx;
    }
}

__device__ int cal_new_idx(int n, int h, int w, int c, Dim dim, DimOrder order)
{
    if (order == NHWC)
    {
        return n * dim.H * dim.W * dim.C + h * dim.W * dim.C + w * dim.C + c;
    }
    else if (order == NCWH)
    {
        return n * dim.C * dim.W * dim.H + c * dim.W * dim.H + w * dim.H + h;
    }
    else if (order == NCHW)
    {
        return n * dim.C * dim.H * dim.W + c * dim.H * dim.W + h * dim.W + w;
    }
    return 0;
}
}

__global__ void Resize_kernel(const uint8_t* src, uint8_t* dst, Dim old_dim, Dim new_dim, 
                                DimOrder dim_order, float fw, float fh, size_t unit_count)
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= unit_count) return;

    int n, h, w, c;
    resize::cal_dim(idx, &n, &h, &w, &c, new_dim, dim_order);

    float old_w = (float)w * fw;
    float old_h = (float)h * fh;

    int w1 = int(old_w);
    int w2 = int(old_w) + 1;
    int h1 = int(old_h);
    int h2 = int(old_h) + 1;

    if (w1 < 0) w1 = 0;
    if (h1 < 0) h1 = 0;
    if (w2 >= old_dim.W) w2 = old_dim.W - 1;
    if (h2 >= old_dim.H) h2 = old_dim.H - 1;

    int idx_1 = resize::cal_new_idx(n, h1, w1, c, old_dim, dim_order);
    int idx_2 = resize::cal_new_idx(n, h1, w2, c, old_dim, dim_order);
    int idx_3 = resize::cal_new_idx(n, h2, w1, c, old_dim, dim_order);
    int idx_4 = resize::cal_new_idx(n, h2, w2, c, old_dim, dim_order);

    float f_w1 = w1;
    float f_w2 = w2;
    float f_h1 = h1;
    float f_h2 = h2;
    float f1 = src[idx_1];
    float f2 = src[idx_2];
    float f3 = src[idx_3];
    float f4 = src[idx_4];

    float val_12 = (old_w - f_w1) * f1 / (f_w2 - f_w1) + (f_w2 - old_w) * f2 / (f_w2 - f_w1);
    float val_34 = (old_w - f_w1) * f3 / (f_w2 - f_w1) + (f_w2 - old_w) * f4 / (f_w2 - f_w1);
    float val = (old_h - f_h1) * val_12 / (f_h2 - f_h1) + (f_h2 - old_h) * val_34 / (f_h2 - f_h1);

    uint8_t val_u = uint8_t(val + 0.5);
    dst[idx] = val_u;
}

void Resize(const uint8_t* src, uint8_t* dst, Dim old_dim, Dim new_dim, 
                DimOrder dim_order, size_t unit_count)
{    
    dim3 block(1024);
    dim3 grid((unit_count - 1) / block.x + 1);
    float fw = (float)old_dim.W / (float)new_dim.W;
    float fh = (float)old_dim.H / (float)new_dim.H;
    Resize_kernel<<<grid, block>>>(src, dst, old_dim, new_dim, dim_order,
                                    fw, fh, unit_count);
    CHECK(cudaDeviceSynchronize());
    return;
}
